package com.xr.web.controller.miniprogram;

import com.xr.common.annotation.Log;
import com.xr.common.constant.MiniProgramConstants;
import com.xr.common.constant.OrderStatusConstants;
import com.xr.common.constant.StaffConstants;
import com.xr.common.constant.WorkStatusConstants;
import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.common.core.page.TableDataInfo;
import com.xr.common.enums.BusinessType;
import com.xr.miniprogram.domain.MiniProgramOrderUser;
import com.xr.miniprogram.domain.MiniProgramServiceLog;
import com.xr.miniprogram.domain.MiniProgramStaff;
import com.xr.miniprogram.domain.MiniProgramUser;
import com.xr.miniprogram.service.IMiniProgramOrderUserService;
import com.xr.miniprogram.service.IMiniProgramServiceLogService;
import com.xr.miniprogram.service.IMiniProgramStaffService;
import com.xr.miniprogram.service.IMiniProgramUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description: 服务工人模块接口
 *
 * @author: wls
 * @date: 2023/11/9 9:37
 * @version:1.0
 */
@Api("服务工人模块接口")
@RestController
@RequestMapping("/mini/staff")
public class MpStaffController extends BaseController {

    @Resource
    IMiniProgramStaffService staffService;

    @Resource
    IMiniProgramServiceLogService serviceLogService;

    @Resource
    IMiniProgramOrderUserService orderUserService;

    @Resource
    IMiniProgramUserService userService;

    /**
     * 根据userId获取工人被分配的待完成订单服务
     * @param userId
     * @return
     */
    @ApiOperation("根据userId获取工人被分配的待完成订单服务")
    @PreAuthorize("@ss.hasPermi('mini:orderuser:list')")
    @RequestMapping("/getStaffOrderList/{userId}")
    public AjaxResult getStaffOrderList(@PathVariable("userId") Long userId){
        return success(staffService.getStaffOrderList(userId,null, WorkStatusConstants.END_SERVICE));
    }

    /**
     * 根据orderUserId获取工人
     * @param orderUserId
     * @return
     */
    @ApiOperation("根据orderUserId获取工人")
    @PreAuthorize("@ss.hasPermi('mini:staff:query')")
    @RequestMapping("/getStaffByOrderUserId/{orderUserId}")
    public AjaxResult getStaffByOrderUserId(@PathVariable("orderUserId") Long orderUserId){
        MiniProgramOrderUser orderUser = orderUserService.getById(orderUserId);
        MiniProgramServiceLog serviceLog = serviceLogService.getById(orderUser.getServiceLogId());
        Map<String, Object> map = new HashMap<>();
        map.put("serviceLog",serviceLog);
        map.put("staffId",serviceLog.getStaffId());
        return success(map);
    }

    /**
     * 取消被分配订单
     * @param serviceLogId
     * @return
     */
    @ApiOperation("取消被分配订单")
    @PreAuthorize("@ss.hasPermi('mini:staff:edit')")
    @Log(title = "服务工人 取消被分配订单")
    @PostMapping("/deleteStaffOrder/{serviceLogId}")
    public AjaxResult deleteStaffOrder(@PathVariable("serviceLogId") Long serviceLogId){
        MiniProgramOrderUser miniProgramOrderUser = orderUserService.getByServiceLogId(serviceLogId);
        miniProgramOrderUser.setOrderStatusId(OrderStatusConstants.ALLOCATION_ORDER);
        return success(staffService.updateStaffOrder(serviceLogId, 0L) && orderUserService.updateById(miniProgramOrderUser));
    }

    /**
     * 新增服务人员
     * @param miniProgramStaff
     * @return
     */
    @ApiOperation("新增服务人员")
    @PreAuthorize("@ss.hasPermi('mini:staff:add')")
    @RequestMapping("/add")
    public AjaxResult add(@RequestBody MiniProgramStaff miniProgramStaff){
        Long userId = miniProgramStaff.getUserId();
        MiniProgramUser programUser = userService.getById(userId);
        if(programUser==null){
            return warn("没有该用户");
        }
        MiniProgramStaff staff = staffService.getByUserId(userId);
        if(staff!=null){
            return warn("已存在");
        }else{
            return success(staffService.save(miniProgramStaff));
        }
    }

    @ApiOperation("根据id查询服务工人")
    @PreAuthorize("@ss.hasPermi('mini:staff:query')")
    @RequestMapping("/select/{staffId}")
    public AjaxResult select(@PathVariable("staffId") Long staffId){
        MiniProgramStaff miniProgramStaff = staffService.getById(staffId);
        return success(miniProgramStaff);
    }

    @ApiOperation("查询服务工人列表")
    @PreAuthorize("@ss.hasPermi('mini:staff:list')")
    @RequestMapping("/list")
    public TableDataInfo list(MiniProgramStaff staff){
        startPage();
        List<MiniProgramStaff> miniProgramStaff = staffService.selectMiniProgramStaffList(staff);
        return getDataTable(miniProgramStaff);
    }


    /**
     * 修改服务工人
     */
    @ApiOperation("修改服务工人")
    @PreAuthorize("@ss.hasPermi('mini:staff:edit')")
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody MiniProgramStaff miniProgramStaff)
    {
        return toAjax(staffService.updateMiniProgramStaff(miniProgramStaff));
    }
}
